package io.zhengsh.nio.files;

import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;

import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;

public class FilesDemo {

    public static void main(String[] args) {
        Path path = Paths.get("/Users/zhengsh/sourcecode.io" +
                "/zhengsh-vvip/nio/src/main/resources/newdir");

        try {
            Path newDir = Files.createDirectory(path);
        } catch (FileAlreadyExistsException ex) {
            // 目录已经存在
            ex.printStackTrace();
        } catch (IOException e) {
            // 其他异常
            e.printStackTrace();
        }


        Path sourcePath = Paths.get("/Users/zhengsh/sourcecode.io" +
                "/zhengsh-vvip/nio/src/main/resources/newdir/01.txt");

        Path destinationPath = Paths.get("/Users/zhengsh/sourcecode.io" +
                "/zhengsh-vvip/nio/src/main/resources/newdir/01.txt");

        try {
            //Files.copy(sourcePath, destinationPath, StandardCopyOption.REPLACE_EXISTING);
            Files.move(sourcePath, destinationPath, StandardCopyOption.REPLACE_EXISTING);
        } catch (FileAlreadyExistsException ex) {
            // 目录已经存在
            ex.printStackTrace();
        } catch (IOException e) {
            // 其他异常
            e.printStackTrace();
        }


        Path rootPath = Paths.get("c:\\SourceCode");
        String filetoFind = File.separator + "001.txt";

        try {
            Files.walkFileTree(rootPath, new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    String fileString = String.valueOf(file.toAbsolutePath());

                    if (fileString.endsWith(filetoFind)) {
                        System.out.println("file found at path" + file.toAbsolutePath());
                        return FileVisitResult.TERMINATE;
                    }

                    return FileVisitResult.CONTINUE;
                }
            });
        } catch (FileAlreadyExistsException ex) {
            // 目录已经存在
            ex.printStackTrace();
        } catch (IOException e) {
            // 其他异常
            e.printStackTrace();
        }
    }
}
